package blingking.jfinal.orm.tx;

import com.atomikos.icatch.jta.UserTransactionImp;
import com.jfinal.kit.LogKit;
import com.jfinal.plugin.activerecord.ActiveRecordException;
import com.jfinal.plugin.activerecord.NestedTransactionHelpException;
import org.aspectj.lang.ProceedingJoinPoint;

/**
 * @author King
 * @description
 * @Date 2020/3/22
 */
public class XATx {

    public static Object execAOPTx(ProceedingJoinPoint pjp) throws Throwable {
        Object retVal = null;
        UserTransactionImp utx = new UserTransactionImp();
        try {
            utx.begin();
            retVal = pjp.proceed();
            utx.commit();
        } catch (NestedTransactionHelpException e) {
            utx.rollback();
            LogKit.logNothing(e);
        } catch (Throwable t) {
            utx.rollback();
            throw t instanceof RuntimeException ? (RuntimeException) t : new ActiveRecordException(t);
        }
        return retVal;
    }
}
